
import UIKit

class TFLotteryViewController: UIViewController, CAAnimationDelegate {
    @objc public var timer:Timer?

    @objc public let deleteBtn = UIButton()
    //盘顶
    @objc public let turntableImgView = UIImageView()
    //转盘背景
    @objc public let turntableBackgroundImageView = UIImageView()
    @objc public let closeButton = UIButton()
    //指针中心
    @objc public let pointer = UIButton()
    @objc public var count:NSInteger = 0
    //是否已经动画结束  (默认为YES)
    @objc public var finished:Bool = true

    @objc public weak var delegate : TFLotteryViewControllerDelegate?
   
    @objc public var isClose:Bool = false
   
    @objc public let rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
    //是否中奖
    @objc public var iswinner:Bool = false
    //是否公布
    @objc public var ispub:Bool = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
       let viewSize = UIScreen.main.bounds.size as CGSize
        //防止横屏进来
        let width =  viewSize.width > viewSize.height ? viewSize.height : viewSize.width;
        let height = viewSize.width > viewSize.height ? viewSize.width : viewSize.height
        self.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
    
        self.view.backgroundColor = UIColor(red: 5/255.0, green: 0/255.0, blue: 1/255.0, alpha: 0.8)
        
        self.turntableImgView.image = UIImage.init(named: "HTLive.bundle/JBtuopan")
      
        self.turntableImgView.isUserInteractionEnabled = true
        self.view.addSubview(self.turntableImgView)
        
        //TODO:关闭窗口
        self.deleteBtn.setImage(UIImage.init(named: "HTLive.bundle/lottery_close1"), for: .normal)
        self.turntableImgView.addSubview(self.deleteBtn)
     
        
        //转盘背景
        
        self.turntableBackgroundImageView.image = UIImage.init(named: "HTLive.bundle/wheel")
        self.turntableImgView.addSubview(self.turntableBackgroundImageView)
        
        self.pointer.setImage(UIImage.init(named: "HTLive.bundle/pointer"), for: .normal)
        self.pointer.isUserInteractionEnabled = false
        self.turntableImgView.addSubview(self.pointer)

        self.updateFrame()
        
    }
    
     @objc public func updateFrame()
    {
        let viewSize = UIScreen.main.bounds.size as CGSize
//        let width =  viewSize.width > viewSize.height ? viewSize.height : viewSize.width;
//        let height = viewSize.width > viewSize.height ? viewSize.width : viewSize.height
        //横屏
        if (viewSize.width > viewSize.height) {
            
            self.turntableImgView.frame = CGRect(x: (viewSize.width-256)/2, y: (viewSize.height-320)/2, width: 256, height: 320)
          
        self.deleteBtn.frame = CGRect(x: self.turntableImgView.frame.size.width - 30, y: 20, width: 30, height: 30)
         
        self.turntableBackgroundImageView.frame = CGRect(x: (self.turntableImgView.frame.size.width-210)/2.0, y: 35, width: 210, height: 210)
          
            self.pointer.frame = CGRect(x:(self.turntableImgView.frame.size.width-64)/2.0, y: 60+35, width: 64, height: 80)
      
        } else {
            //竖屏
            if (viewSize.width < 375) {
                self.turntableImgView.frame = CGRect(x:(viewSize.width-320)/2, y: (viewSize.height-400)/2, width: 320, height: 400)
             
                self.deleteBtn.frame = CGRect(x: self.turntableImgView.frame.size.width - 60, y: 20, width: 30, height: 30)
          
                self.turntableBackgroundImageView.frame = CGRect(x: (self.turntableImgView.frame.size.width-270)/2.0, y: 36, width: 270, height: 270)
                self.pointer.frame = CGRect(x: (self.turntableImgView.frame.size.width-94)/2.0, y: 112, width: 94, height: 118)
            } else {
                
                self.turntableImgView.frame = CGRect(x:(viewSize.width-375)/2, y:(viewSize.height-467)/2, width: 375, height: 467)
                
                self.deleteBtn.frame = CGRect(x: self.turntableImgView.frame.size.width - 60, y: 20, width: 30, height: 30)
                
    
                self.turntableBackgroundImageView.frame = CGRect(x: (self.turntableImgView.frame.size.width-317)/2.0, y: 42, width: 317, height: 317)
               
                self.pointer.frame = CGRect(x: (self.turntableImgView.frame.size.width-94)/2.0, y: 90+42, width: 94, height: 118)
            }
            
         
            
            
        }
    }
    //控制器的view即将消失的时候调用
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        if (self.timer != nil) {
            self.timer?.invalidate()
            self.timer = nil
        }
       
    }

    //抽奖开始
    @objc public func lotteryStartUI()
    {
        if (self.timer != nil) {
            self.timer?.invalidate()
            self.timer = nil
        }
        self.timer = Timer.scheduledTimer(timeInterval: 0.08, target: self, selector: #selector(recursive), userInfo: nil, repeats: true)
        RunLoop.main.add(self.timer! , forMode: RunLoop.Mode.default)
        
        self.timer!.fire()
        
        self.deleteBtn.setImage(UIImage.init(named: "HTLive.bundle/lottery_close1"), for: .normal)
       
       
    }
    
    @objc func recursive()
    {
        self.startRotationAnimation(number: 2)
    }
    //开始转盘转盘
    @objc func startRotationAnimation(number:NSInteger)
    {
        self.finished =  false
        self.isClose = false
        var turnAngle:Float = 0;//8个奖励分别对应的角度

        let turnsNum:Float = 3;//控制圈数

        let perAngle:Float = Float(.pi/180.0);
        
     
        turnAngle =  360/6
        turnAngle =  turnAngle * Float((7 - number));
        
        let newFloat = turnAngle * perAngle + 360 * perAngle * turnsNum
        
        self.rotationAnimation.toValue = (newFloat)
        
    
        self.rotationAnimation.duration = 3.0
        self.rotationAnimation.isCumulative = true
        self.rotationAnimation.delegate = self
        //由快变慢
        self.rotationAnimation.timingFunction =  CAMediaTimingFunction(name:CAMediaTimingFunctionName.easeOut)

        self.rotationAnimation.fillMode = CAMediaTimingFillMode.forwards
        self.rotationAnimation.isRemovedOnCompletion = false
        self.turntableBackgroundImageView.layer.add(self.rotationAnimation, forKey: "rotationAnimation")
      
        
        // 转盘结束后调用，显示获得的对应奖励
    }
    
    //抽奖结束
    @objc public func lotteryStopUI()
    {
        ////是否中奖
        if (self.iswinner) {
            self.startRotationAnimation(number: 3)
        } else {
            self.startRotationAnimation(number: 2)
        }

        if (self.timer != nil) {
            self.timer?.invalidate()
            self.timer = nil
        }
        self.deleteBtn.setImage(UIImage.init(named: "HTLive.bundle/lottery_close1"), for: .normal)
         
    }

    //动画结束   代理方法
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool)

    {
        self.finished = true

        if (flag) {
            
            
                   if self.delegate != nil && (self.delegate?.responds(to: #selector(TFLotteryViewControllerDelegate.updateLotterView(winner:pub:))))!{
                       
                     
                       self.delegate?.updateLotterView(winner: self.iswinner, pub: self.ispub)
                   }

      
            
        }
        
    }
    //X 点击事件
    @objc public func closeButtonClict1()

    {
         self.isClose = true
         if (self.finished) {
             self.view.removeFromSuperview()
            
         }
    }
    @objc public func closeButtonClict()
    {
         self.isClose = false;
        self.view.removeFromSuperview()
    }
}
@objc protocol TFLotteryViewControllerDelegate : NSObjectProtocol {
    
    func updateLotterView(winner:Bool,pub:Bool)

}
